%% -*- erlang -*-

%% Yo, this is just a test backend for bitcask's multi_backend schema. It's not real
%% If you really care about multi_backend, have a look at riak_kv/priv/multi_backend.schema

%% @doc Storage_backend specifies the Erlang module defining the storage
%% mechanism that will be used on this node.
{mapping, "multi_backend.$name.storage_backend", "riak_kv.multi_backend", [
  {default, leveldb},
  {datatype, {enum, [bitcask, leveldb, memory]}},
  hidden
]}.

{translation,
 "riak_kv.multi_backend",
 fun(Conf, Schema) ->
  GenerateSubConfig = fun(Name, Prefix, ProplistKey, ModuleName) ->
      BackendConfigName = ["multi_backend", Name],
      BackendConfigPrefix = BackendConfigName ++ [Prefix],
      SubConf = [ begin
          {lists:nthtail(2, Key), Value}
      end || {Key, Value} <- cuttlefish_variable:filter_by_prefix(BackendConfigPrefix, Conf)],

      case cuttlefish_generator:map(Schema, SubConf) of
        {error, _Phase, {error, Errors}} ->
            [ io:format("~s~n", [E]) || {error, E} <- Errors ],
            cuttlefish:invalid(
              lists:flatten(io_lib:format(
                "Error processing multi_backend configuration for backend ~s", [Name])));
        BackendProplist ->
          Proplist = lists:foldl(
          fun(K, Acc) ->
            proplists:get_value(K, Acc, [])
          end,
          BackendProplist, ProplistKey),
          {ModuleName, Proplist}
      end
  end,
  %% group by $name into list, also cut the "multi_backend.$name" off every key
  BackendNames = cuttlefish_variable:fuzzy_matches(["multi_backend","$name","storage_backend"], Conf),
  %% for each in list, case statement on backend type
  Backends = [ begin
    BackendConfigName = ["multi_backend", Name],
    {BackendModule, BackendConfig} = case cuttlefish:conf_get(BackendConfigName ++ ["storage_backend"], Conf) of
      leveldb ->
        GenerateSubConfig(Name, "leveldb", [eleveldb], riak_kv_eleveldb_backend)
      end,
    {list_to_binary(Name),  BackendModule, BackendConfig}
  end || {"$name", Name} <- BackendNames],
  case Backends of
      [] -> throw(unset);
      _ -> Backends
  end
 end
}.
